package ${package}.${moduleName}.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.citic.office.lowcode.util.CopyUtil;
import com.citic.office.common.core.domain.R;
import com.citic.office.common.core.web.controller.BaseController;
import com.citic.office.common.core.web.page.TableDataInfo;
import com.citic.office.common.log.enums.BusinessType;
import com.citic.office.common.log.annotation.Log;
import com.citic.office.common.security.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import ${package}.${moduleName}.model.entity.vo.${className}Vo;
import ${package}.${moduleName}.base.ResultEnum;
import ${package}.${moduleName}.model.entity.${className};
import ${package}.${moduleName}.service.${className}Service;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ${comments}
 *
 * @author ${author}
 * @date ${datetime}
 */
@RestController
@AllArgsConstructor
@RequestMapping("/${pathName}" )
@Api(value = "${comments}", tags = "${comments}-app接口")
public class ${className}Api extends BaseController{

    private final ${className}Service ${classname}Service;

    /**
    * 分页查询
    * @param pageNum 当前页数
    * @param pageSize 每页数据量
    * @param ${classname} 查询条件
    * @return 查询结果
    */
//    @RequiresPermissions("lowcode:${pathName}:page")
    @Log(title = "${comments}", businessType = BusinessType.QUERY)
    @ApiOperation(value = "分页查询${comments}", notes = "分页查询${comments}")
    @GetMapping("/page")
    public TableDataInfo<${className}Vo> get${className}Page(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long pageNum,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "查询条件") ${className} ${classname}) {
        startPage();
        return getDataTable(${classname}Service.list(Wrappers.query(${classname})), ${className}Vo.class);
    }


    /**
    * 通过id查询${comments}
    * @param id id
    * @return Result
    */
    //    @RequiresPermissions("lowcode:${pathName}:getById")
    @ApiOperation(value = "通过id查询${comments}", notes = "通过id查询${comments}")
    @Log(title = "${comments}", businessType = BusinessType.QUERY)
    @GetMapping
    public R<${className}Vo> getById(@ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        return R.ok(BeanUtil.copyProperties(${classname}Service.getById(id),${className}Vo.class));
    }

    /**
    * 新增${comments}
    * @param ${classname} ${comments}
    * @return Result
    */
    @ApiOperation(value = "新增${comments}", notes = "新增${comments}")
    @PostMapping
    @Log(title = "${comments}", businessType = BusinessType.INSERT)
    //   @RequiresPermissions("lowcode:${pathName}:save")
    public R save(@RequestBody @Valid ${className} ${classname}) {
        Assert.isTrue(${classname}Service.save(${classname}), "操作失败");
        return R.ok("新增成功");
    }

    /**
    * 修改${comments}
    * @param ${classname} ${comments}
    * @return Result
    */
    @PutMapping
    @ApiOperation(value = "修改${comments}", notes = "修改${comments}")
    @Log(title = "${comments}", businessType = BusinessType.UPDATE)
    //    @RequiresPermissions("lowcode:${pathName}:edit")
    public R updateById(@RequestBody @Valid ${className} ${classname}) {
        Assert.isTrue(${classname}Service.updateById(${classname}), "操作失败");
        return R.ok("修改成功");
    }

    /**
    * 通过id删除${comments}
    * @param ids id集合
    * @return Result
    */
    @DeleteMapping
    @ApiOperation(value = "通过id删除${comments}", notes = "通过id删除${comments}")
    //   @RequiresPermissions("lowcode:${pathName}:del")
    @Log(title = "${comments}", businessType = BusinessType.DELETE)
    public R<Boolean> removeById(
            @ApiParam(value = "id，多个id用英文逗号分隔", required = true) @RequestParam("ids") String ids) {
        String[] split = ids.split(",");
        if (split.length < 1) {
            return R.fail(ResultEnum.CRUD_PARAM_NOT.getDesc());
        }
        List<Long> idList = new ArrayList<Long>();
        Arrays.stream(split).forEach(s -> {
            if (StrUtil.isNotBlank(s)) {
                idList.add(Long.parseLong(s));
            }
        });
        Assert.isTrue(${classname}Service.removeByIds(idList), "操作失败");
        return R.ok();
    }
}